package com.blb.demo3;

public class VolatileDemo {

    public volatile boolean flag=true;

    public void testVolatile(){
        System.out.println("程序执行");
        while (flag){

        }
        System.out.println("程序结束");
    }


    public static void main(String[] args) throws InterruptedException {
        VolatileDemo volatileDemo = new VolatileDemo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                volatileDemo.testVolatile();
            }
        }).start();

        Thread.sleep(5000L);
        volatileDemo.flag=false;
        System.out.println("主线程结束");


    }


}
